﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Syrinx.Gui.AspNet.Ajax;

namespace Articles.Gui.AspNet
{
	public class SalBizObjDisplay : Swaf.Gui.AspNet2.UserControl, IBizObjDisplay, IAjaxClientControl
	{
		protected static string s_setupScript = @"
<script>//{0}:
{1}cc = new ClientControl(document.getElementById('{0}'), setSalDetailsState, getSalDetailsState);
{1}cc.id = '{0}';
{2}BizProc.addClientControl({1}cc);
</script>
";
		protected static string s_globalScript = @"
function setSalDetailsState(stateEl)
{
	var html = stateEl.getAttribute('html');
	if(html != null && html != '')
		this.dhtmlObj.innerHTML = html;

	var script = stateEl.getAttribute('script');
	try{eval(script);}catch(e){}
}

function getSalDetailsState(stateEl)
{
}
";

		protected IBizObj m_salInfo;

		public SalBizObjDisplay()
		{
		}
		public SalBizObjDisplay(ActiveWidget widget) : base(widget) 
		{
		}

		public override string ContentName
		{
			get
			{
				//if(Cmn.IsEmpty(base.ContentName))
					return "Articles.Gui.AspNet.StandAloneArticleListDisplay";
				//return base.ContentName;
			}
			set
			{
				base.ContentName = value;
			}
		}

		protected override bool ShouldCacheStructure
		{
			get
			{
				return false;
			}
		}
		protected override System.IO.TextReader Html
		{
			get
			{
				string baseHtml = @"<%@ Register Assembly=""SwafCore"" Namespace=""SwafCore"" TagPrefix=""swf"" %>
<%@ Register Assembly=""ScsAspNet"" Namespace=""Syrinx.Gui.AspNet"" TagPrefix=""syx"" %>
<%@ Register Assembly=""ScsAspNet"" Namespace=""Syrinx.Gui.AspNet.Ajax"" TagPrefix=""snc"" %>
<%@ Register Assembly=""NitobiControls"" Namespace=""Nitobi"" TagPrefix=""ntb"" %>
<%@ Register Assembly=""ScsAspNet"" Namespace=""Articles.Gui.AspNet"" TagPrefix=""art"" %>
<%@ Register Assembly=""ScsAspNet"" Namespace=""eCom.Gui.AspNet"" TagPrefix=""ecom"" %>
";
				if (m_salInfo != null)
				{
					baseHtml += (string)m_salInfo.get("salHtml");
					if (BPC != null)
					{
						string procId = "g" + (string)m_salInfo.get("id");
						BPC.PageState.BizProcStates.Remove(procId);
						List<string> ids = new List<string>();
						foreach (string widgetId in BPC.PageState.WidgetStates.Keys)
							if (widgetId.StartsWith(UniqueID) && widgetId.Length > UniqueID.Length)
								ids.Add(widgetId);
						foreach (string widgetId in ids)
							BPC.PageState.WidgetStates.Remove(widgetId);
					}
				}
				return new System.IO.StringReader(baseHtml);
			}
		}

		public override BizProcWidgetType BizProcWidgetType
		{
			get
			{
				return BizProcWidgetType.disp;
			}
			set
			{
			}
		}

		public Swaf.Security.Privileges EditPrivilege {get{return Swaf.Security.Privileges.Edit;}set{}}

		public void displayBizObj(IBizObj salObj)
		{
			m_salInfo = salObj;
			clearControls();
			createControls();
			//setupOptionsFromBizObj(salObj);
		}

		public bool isDisplayingContent{get { return true; }}

		public bool isSelected{get { return false; }}

		public void updateBizObj(IBizObj album){}

		public event BizObjSelectedInfo BizObjSelected;


		#region IAjaxClientControl Members

		public bool CreateOnLoad
		{
			set {  }
		}

		public void setState(System.Xml.XmlElement clientState)
		{
		}

		public void getState(System.Xml.XmlElement clientState, string pageHtml)
		{
			if (pageHtml != null)
			{
				StringBuilder scriptBuff = new StringBuilder(/*generatePushPullClientScriptMethods(true)*/);
				Regex r = new Regex(string.Format("<!--{0}-->(.*?)<!--{0}-->", ClientID.Replace("$", "\\$")), RegexOptions.IgnoreCase | RegexOptions.Singleline);
				Match m = r.Match(pageHtml);
				if (m.Success)
				{
					StringBuilder htmlBuff = new StringBuilder();
					string myHtml = m.Groups[1].Value;
					r = new Regex("<script.*?>(.*?)</script>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
					MatchCollection mc = r.Matches(myHtml);
					int lastPos = 0;
					foreach (Match m2 in mc)
					{
						htmlBuff.Append(myHtml.Substring(lastPos, m2.Index - lastPos));
						//if (!m2.Groups[1].Value.StartsWith("//" + ClientID + ":"))
						scriptBuff.Append(m2.Groups[1].Value);
						lastPos = m2.Index + m2.Length;
					}
					//if (lastPos != 0)
					htmlBuff.Append(myHtml.Substring(lastPos));

					clientState.SetAttribute("html", htmlBuff.ToString());
					clientState.SetAttribute("script", scriptBuff.ToString());
				}
			}
		}

		public void processAjaxEvent(System.Xml.XmlElement eventInfo)
		{
		}

		public bool NeedToGenerateHtmlWithServerControls
		{
			get { return true; }
		}

		#endregion

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			Page.ClientScript.RegisterClientScriptBlock(typeof(SalBizObjDisplay), "GlobalBizObj", s_globalScript, true);
		}
		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			writer.Write("<!--{0}-->", ClientID);
			base.Render(writer);
			writer.Write("<!--{0}-->", ClientID);
			ActiveBizProc bizproc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);
			writer.Write(s_setupScript, WidgetId, ClientID, bizproc.BizProc.Id);
		}
	}
}
